package com.hermit.qmst.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hermit.qmst.R;
import com.hermit.qmst.mode.FindListViewInfo;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.ImageDownLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/9 0009.
 */
public class FindAdapter extends BaseAdapter {
    private Context mContext;
    public List<FindListViewInfo> data;

    public FindAdapter(Context mContext) {
        this.mContext = mContext;
        data = new ArrayList<FindListViewInfo>();
    }
    @Override
    public int getCount() {
        return data.size();
//        return 7;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       Holder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_find, null);
            holder = new Holder();
            holder.v_line_width = (View) convertView.findViewById(R.id.v_line_width);
            holder.ll_item_height = (LinearLayout) convertView.findViewById(R.id.ll_item_height);

            holder.lvImg=(ImageView)convertView.findViewById(R.id.iv_find_img);
            holder.title=(TextView) convertView.findViewById(R.id.tv_find_title);
            holder.tv_find_describle=(TextView) convertView.findViewById(R.id.tv_find_describle);

            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        if (position == 3) {
            holder.ll_item_height.setVisibility(View.VISIBLE);
            holder.v_line_width.setVisibility(View.GONE);
        } else if (position == 6) {
            holder.v_line_width.setVisibility(View.GONE);
        } else {
            holder.v_line_width.setVisibility(View.VISIBLE);

        }
        holder.tv_find_describle.setText(data.get(position).getDescrible());
        holder.title.setText(data.get(position).getTitle());
        final  ImageView iv= holder.lvImg;
        String url = data.get(position).getLogo();
        ImageDownLoader imageDownLoader = new ImageDownLoader(mContext);
        Bitmap bitmap = imageDownLoader.downloadImage(Common.getDialImageDirPath(),url,new ImageDownLoader.onImageLoaderListener() {
            @Override
            public void onImageLoader(Bitmap bitmap, String url) {
                Drawable drawable = new BitmapDrawable(mContext.getResources(),bitmap);
                iv.setBackgroundDrawable(drawable);
            }
        });

        if (bitmap != null){
            iv.setBackgroundDrawable(new BitmapDrawable(mContext.getResources(),bitmap));
        }
        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class Holder {
        TextView title;
        TextView tv_find_describle;
        ImageView lvImg;
        View v_line_width;
        LinearLayout ll_item_height;
    }
}
